<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 小虎哥 <1105415366@qq.com>
 * Date: 2018-06-28
 */

namespace weapp\MyLivechat\controller;

use think\Db;
use app\common\controller\Weapp;
use weapp\MyLivechat\model\MyLivechatModel;

/**
 * 插件的控制器
 */
class MyLivechat extends Weapp
{

    /**
     * 实例化模型
     */
    private $model;

    /**
     * 插件基本信息
     */
    private $weappInfo;

    private $scan_url = '/weapp/MyLivechat/template/skin/images/weixin.jpg';

    /**
     * 构造方法
     */
    public function __construct()
    {
        parent::__construct();
        $this->model = new MyLivechatModel;
        $this->Weapp_db = Db::name('weapp'); 
        /*插件基本信息*/
        $this->weappInfo = $this->getWeappInfo();
        $this->assign('weappInfo', $this->weappInfo);
        /*--end*/
    }

    /**
     * 插件使用指南
     */
    public function doc()
    {
        return $this->fetch('doc');
    }

    /**
     * 插件前台展示 - show钩子方法
     * @param  mixed $params 传入的参数
     */
    public function show($params = null)
    {
        $res = $this->Weapp_db->where('code','MyLivechat')->field('data')->find();
        $MyLivechat = unserialize($res['data']);       
        $this->assign('MyLivechat', $MyLivechat);
        echo $this->fetch('show');
    }

    /**
     * 插件后台管理 - 列表
     */
    public function index()
    {        
        if (IS_POST) {
            $data = input('post.');
            if (!empty($data['code'])) { 
                $data['seo_myLivechat'] = str_replace('ｓｃｒｉｐｔ','script',$data['seo_myLivechat']);
                $saveData = array(
                    'data' => serialize($data),
                    'tag_weapp' => $data['tag_weapp'],
                    'update_time' => getTime(),
                );
                $r = Db::name('weapp')->where(array('code' => $data['code']))->update($saveData);
                if ($r) {
                    adminLog('编辑' . $this->weappInfo['name'] . '成功'); // 写入操作日志
                    $this->success("操作成功!", weapp_url('MyLivechat/MyLivechat/index'));
                }
            }
        }
        $res = $this->Weapp_db->where('code','MyLivechat')->field('data')->fetchSql(false)->find();                
        $MyLivechat = unserialize($res['data']);          
        $this->assign('MyLivechat', $MyLivechat);
        return $this->fetch('index');
    }
}